package org.ltc.alliance.controller;

import org.ltc.alliance.constant.StatusConst;
import org.ltc.alliance.constant.WebConst;
import org.ltc.alliance.model.ClubDomain;
import org.ltc.alliance.model.FavDomain;
import org.ltc.alliance.service.club.ClubService;
import org.ltc.alliance.service.fav.FavService;
import org.ltc.alliance.utils.APIResponse;
import org.ltc.alliance.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/fav")
public class FavController extends BaseController{

    @Autowired
    private FavService favService;
    @Autowired
    private ClubService clubService;

    @RequestMapping("/check-out")
    @ResponseBody
    public APIResponse<?> checkOutClub(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody String body) {
        Map<String, String> map = GsonUtils.getMapFromJson(body, HashMap.class);
        String clubId = map.get("clubId");

        String uid = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(WebConst.USER_IN_COOKIE)){
                uid = cookie.getValue();
            }
        }

        FavDomain favDomain = new FavDomain();
        favDomain.setCid(Integer.parseInt(clubId));
        favDomain.setUid(Integer.parseInt(uid));
        favDomain.setFstatus(StatusConst.Fav.CHECK_OUT);

        try {
            favService.saveFavInfo(favDomain);
        }
        catch (Exception e) {
            return APIResponse.fail(e.toString());
        }
        return APIResponse.success();
    }

    @RequestMapping("/uncheck-out")
    @ResponseBody
    public APIResponse<?> unCheckOutClub(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody String body) {
        Map<String, String> map = GsonUtils.getMapFromJson(body, HashMap.class);
        String clubId = map.get("clubId");

        String uid = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(WebConst.USER_IN_COOKIE)){
                uid = cookie.getValue();
            }
        }

        FavDomain favDomain = new FavDomain();
        favDomain.setCid(Integer.parseInt(clubId));
        favDomain.setUid(Integer.parseInt(uid));
        favDomain.setFstatus(StatusConst.Fav.NULL);

        try {
            favService.saveFavInfo(favDomain);
        }
        catch (Exception e) {
            return APIResponse.fail(e.toString());
        }
        return APIResponse.success();
    }

    @RequestMapping("/user-check-list")
    @ResponseBody
    public APIResponse<?> getUserCheckList(
            HttpServletRequest request,
            HttpServletResponse response) {
        String uid = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(WebConst.USER_IN_COOKIE)){
                uid = cookie.getValue();
            }
        }
        try {
            List<FavDomain> favList = favService.getFavListByUid(Integer.parseInt(uid),
                    StatusConst.Fav.CHECK_OUT);
            List<ClubDomain> clubs = new ArrayList<>();
            for (FavDomain fav : favList){
                clubs.add(clubService.getClubByCid(fav.getCid()));
            }
            return APIResponse.success(clubs);
        }
        catch (Exception e){
            return APIResponse.fail(e.toString());
        }
    }



}
